programming4us
           
 
 
SQL Server

SQL Server 2008 : Viewing and Modifying Data (part 1) - Creating Views

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
10/17/2010 5:38:40 PM
A view is a database object that represents a saved SELECT statement and is also referred to as a virtual or logical table. Views can be queried in the same way as tables, and some types of views can be updated, too. Using views instead of tables can greatly simplify data access and decouple client applications from the underlying tables containing actual data. With the appropriate use of views, it is possible to completely change the schema of the database and redesign the tables without breaking any client applications. Think of views as an abstract interface between your physical database tables and the people or applications querying them.

Creating Views

SQL Server 2008 allows you to create views of the following types:

  • Standard view This view is based on one or more base tables. The view may include joins, filter restrictions (using the WHERE clause), and row count restrictions (using the TOP and ORDER BY clauses). You cannot use the ORDER BY clause in a view without specifying the TOP clause as well.

  • Updateable view A view that is based on a single underlying table can be updated directly. Executing INSERT, UPDATE, DELETE, and MERGE statements on this type of view will affect the data in the underlying table. You can also define an INSTEAD OF INSERT, INSTEAD OF UPDATE, and INSTEAD OF DELETE trigger on any view, which will perform a particular action when you attempt to insert, update, or delete data in the view.

  • Indexed view Sometimes it is valuable to create one or more indexes on a view in order to optimize the time it takes to query the view. Indexes can be created on views using standard CREATE INDEX syntax. Indexed views must be created with the SCHEMABINDING option (see the “Using the SCHEMABINDING Option to Lock in a View’s Underlying Schema” sidebar).

  • Partitioned view A partitioned view joins data that is spread across a table partitioned horizontally—for example, if you have partitioned a table by OrderDate to store orders from five years ago and earlier in one partition, orders created within the last five years in another partition, and orders created this year in yet another partition. A partitioned view will join all the partitions together into one Orders virtual table containing data from all three partitions.

To create a view, use the CREATE VIEW statement syntax shown in Example 1.

Example 1. CREATE VIEW Statement—Syntax
CREATE VIEW [schema_name].view_name [(column_names)]
[ WITH ENCRYPTION | SCHEMABINDING ]
AS select_statement
[ WITH CHECK OPTION ]

Specifying the column_names in a view definition allows you to assign names to computed columns or to rename columns produced by the SELECT statement. This is useful for calculated columns and columns that may have ambiguous names. If you don’t specify explicit column names, the view columns will inherit the same names as the columns in the SELECT statement.

Specifying the WITH ENCRYPTION option encrypts the view definition. This also prevents the view from being used in replication.

Configuring & Implementing...: Using the SCHEMABINDING Option to Lock in a View’s Underlying Schema

Views are named SELECT statements and include one or more columns from one or more tables. What will happen if a column or table referenced by a view is dropped from the database? The view will become invalid and will return an error the next time it is queried. To lock the view into the schema objects on which it relies, add the WITH SCHEMABINDING option to your CREATE VIEW statement.

This option ensures that any table or column referenced by this view cannot be dropped or altered, until the view itself is dropped. This applies only to columns referenced by the view. You can freely add and remove columns from underlying tables, as long as they are not used in the view.

Only specify the SCHEMABINDING option when the view references tables from a single database. You must specify the SCHEMABINDING option if you wish to build indexes on the view you are creating.


Example 2 creates a view based on the Stars table using the SCHEMABINDING option. We then attempt to alter the underlying structure of the base table but receive an error. Figure 1 demonstrates how the same view can be created using the graphical view designer in SQL Server Management Studio.

Figure 1. Creating a View Using SQL Server Management Studio


Example 2. Working with Views
CREATE VIEW MyStarsView WITH SCHEMABINDING
AS SELECT StarName, StarType FROM dbo.Stars
WHERE SolarMass >=1;
GO
SELECT * FROM MyStarsView;
-- Results:
-- StarName StarType
-- ---------- --------------------------------------------------
-- Deneb White supergiant
-- Pollux Orange Giant
-- Sun Yellow dwarf
ALTER TABLE Stars
DROP COLUMN StarType;
GO
-- Results:
--Msg 5074, Level 16, State 1, Line 1
-- The object 'MyStarsView' is dependent on column 'StarType'.
-- Msg 5074, Level 16, State 1, Line 1
-- ALTER TABLE DROP COLUMN StarType failed because one or more objects
access this column.
-- This view is updateable, as it is based upon only one base table
UPDATE MyStarsView
SET StarType = 'White Supermassive Giant'
WHERE StarType = 'White supergiant'
GO
SELECT * FROM MyStarsView;
-- Results:
-- StarName StarType
-- ---------- --------------------------------------------------
-- Deneb White Supermassive Giant
-- Pollux Orange Giant
-- Sun Yellow dwarf



Other -----------------
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us